#!/usr/bin/ruby

@Length = 2000

def trim_file(in_f, out_f)
	@in_file = File.open( in_f )
	@out_file = File.open( out_f, 'w' )

	
	count = 0
	@out_file.write(next_gene)
	while !@in_file.eof?
		l = @in_file.readline.strip
		if( (count + l.size) > @Length )
			@out_file.write( l.slice(0, @Length - count) + "\n" )

			count = 0
			@out_file.write(next_gene)
		else
			count += l.size
			@out_file.write(l + "\n")
		end
	end
	
	@in_file.close
	@out_file.close
end

def next_gene
	while( ! @in_file.eof?) 
		l = @in_file.readline
		return l if (l =~ /^>/)
	end

	return nil
end

if(ARGV != nil)
	case(ARGV.length)
	when 1
		trim_file( ARGV[0], "temp.txt" )
	when 2
		trim_file( ARGV[0], ARGV[1] )
	when 3
		@Length = ARGV[3].to_i
		trim_file( ARGV[0], ARGV[1] )
	else
		puts "Invalid number of arguments"
	end
end
